package com.chkj.fbook.business.controller;

import java.util.List;
import java.util.Map;

import com.chkj.fbook.common.utils.PageUtils;
import com.chkj.fbook.common.utils.Query;
import com.chkj.fbook.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chkj.fbook.business.domain.RoomDO;
import com.chkj.fbook.business.service.RoomService;
import com.chkj.fbook.business.service.SeatService;
import com.chkj.fbook.common.annotation.Log;
import com.chkj.fbook.common.annotation.Permission;

/**
 * 房间管理表
 * 
 * @author shh_he
 * @email 980027164@qq.com
 * @date 2023-10-02 21:43:16
 */
 
@Controller
@RequestMapping("/business/room")
public class RoomController {

	@Autowired
	private RoomService roomService;
	
	@Autowired
	private SeatService seatService;
	
	@Log("房间管理表-去到列表页面")
	@GetMapping()
	@Permission(name = "business:room:room")
	public String Room(){
	    return "business/room/room";
	}
	
	@Log("房间管理表-获取列表信息分页")
	@ResponseBody
	@GetMapping("/list")
	@Permission(name = "business:room:room")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RoomDO> roomList = roomService.list(query);
		int total = roomService.count(query);
		PageUtils pageUtils = new PageUtils(roomList, total);
		return pageUtils;
	}
	
	@Log("房间管理表-去到新增页面")
	@GetMapping("/add")
	@Permission(name = "business:room:add")
	public String add(){
	    return "business/room/add";
	}

	@Log("房间管理表-去到修改页面")
	@GetMapping("/edit/{id}")
	@Permission(name = "business:room:edit")
	public String edit(@PathVariable("id") String id,Model model){
		RoomDO room = roomService.get(id);
		model.addAttribute("room", room);
	    return "business/room/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("房间管理表-保存操作")
	@ResponseBody
	@PostMapping("/save")
	@Permission(name = "business:room:add")
	public R save(RoomDO room){
		
		if(StringUtils.isBlank(room.getName())) {
			return R.error("门店/房间（名称）不能为空！");
		}
		if(room.getSort()==null) {
			return R.error("房间排序不能为空！");
		}
		
		if(roomService.save(room)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("房间管理表-修改操作")
	@ResponseBody
	@RequestMapping("/update")
	@Permission(name = "business:room:edit")
	public R update( RoomDO room){
		
		if(StringUtils.isBlank(room.getName())) {
			return R.error("门店/房间（名称）不能为空！");
		}
		if(room.getSort()==null) {
			return R.error("房间排序不能为空！");
		}
		
		roomService.update(room);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("房间管理表-删除操作")
	@PostMapping( "/remove")
	@ResponseBody
	@Permission(name = "business:room:remove")
	public R remove( String id){
		
		int count = seatService.getSeatCountByRoomId(id);
		if(count>0) {
			return R.error("该房间下面存在桌位/座位绑定，不能删除！");
		}
		
		if(roomService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}

}
